{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import numpy as np\n",
    "import csv\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_path = r'G:\\Deep_Learning\\kaggle\\global-wheat-detection\\dataset\\train.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "    image_id  width  height                         bbox   source\n0  b6ab77fd7   1024    1024   [834.0, 222.0, 56.0, 36.0]  usask_1\n1  b6ab77fd7   1024    1024  [226.0, 548.0, 130.0, 58.0]  usask_1\n2  b6ab77fd7   1024    1024  [377.0, 504.0, 74.0, 160.0]  usask_1\n3  b6ab77fd7   1024    1024  [834.0, 95.0, 109.0, 107.0]  usask_1\n4  b6ab77fd7   1024    1024  [26.0, 144.0, 124.0, 117.0]  usask_1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>image_id</th>\n      <th>width</th>\n      <th>height</th>\n      <th>bbox</th>\n      <th>source</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b6ab77fd7</td>\n      <td>1024</td>\n      <td>1024</td>\n      <td>[834.0, 222.0, 56.0, 36.0]</td>\n      <td>usask_1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b6ab77fd7</td>\n      <td>1024</td>\n      <td>1024</td>\n      <td>[226.0, 548.0, 130.0, 58.0]</td>\n      <td>usask_1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b6ab77fd7</td>\n      <td>1024</td>\n      <td>1024</td>\n      <td>[377.0, 504.0, 74.0, 160.0]</td>\n      <td>usask_1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b6ab77fd7</td>\n      <td>1024</td>\n      <td>1024</td>\n      <td>[834.0, 95.0, 109.0, 107.0]</td>\n      <td>usask_1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b6ab77fd7</td>\n      <td>1024</td>\n      <td>1024</td>\n      <td>[26.0, 144.0, 124.0, 117.0]</td>\n      <td>usask_1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "df = pd.read_csv(csv_path)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_bbox(df):\n",
    "    ids = []\n",
    "    values = []\n",
    "    imd = np.unique(df['image_id'])\n",
    "    df['bbox'] = df['bbox'].apply(lambda x: eval(x))\n",
    "    # for image_id in os.listdir(train_dir):\n",
    "    #     image_id = image_id.split('.')[0]\n",
    "    #     if image_id not in imd :\n",
    "    #         ids.append(image_id)\n",
    "    #         values.append(str([-1,-1,-1,-1]))\n",
    "    # new_df = {'image_id':ids, 'bbox':values}\n",
    "    # new_df = pd.DataFrame(new_df)\n",
    "    df = df[['image_id','bbox']]\n",
    "    # df.append(new_df)\n",
    "    df = df.reset_index(drop=True)\n",
    "    df['x'] = df['bbox'].apply(lambda x: x[0])\n",
    "    df['y'] = df['bbox'].apply(lambda x: x[1])\n",
    "    df['w'] = df['bbox'].apply(lambda x: x[2])\n",
    "    df['h'] = df['bbox'].apply(lambda x: x[3])\n",
    "\n",
    "    df.drop(columns=['bbox'],inplace=True)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "    image_id      x      y      w      h\n0  b6ab77fd7  834.0  222.0   56.0   36.0\n1  b6ab77fd7  226.0  548.0  130.0   58.0\n2  b6ab77fd7  377.0  504.0   74.0  160.0\n3  b6ab77fd7  834.0   95.0  109.0  107.0\n4  b6ab77fd7   26.0  144.0  124.0  117.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>image_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>w</th>\n      <th>h</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b6ab77fd7</td>\n      <td>834.0</td>\n      <td>222.0</td>\n      <td>56.0</td>\n      <td>36.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b6ab77fd7</td>\n      <td>226.0</td>\n      <td>548.0</td>\n      <td>130.0</td>\n      <td>58.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b6ab77fd7</td>\n      <td>377.0</td>\n      <td>504.0</td>\n      <td>74.0</td>\n      <td>160.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b6ab77fd7</td>\n      <td>834.0</td>\n      <td>95.0</td>\n      <td>109.0</td>\n      <td>107.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b6ab77fd7</td>\n      <td>26.0</td>\n      <td>144.0</td>\n      <td>124.0</td>\n      <td>117.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "df_ = process_bbox(df)\n",
    "\n",
    "df_.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "       x      y      w      h\n0  834.0  222.0   56.0   36.0\n1  226.0  548.0  130.0   58.0\n2  377.0  504.0   74.0  160.0\n3  834.0   95.0  109.0  107.0\n4   26.0  144.0  124.0  117.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>x</th>\n      <th>y</th>\n      <th>w</th>\n      <th>h</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>834.0</td>\n      <td>222.0</td>\n      <td>56.0</td>\n      <td>36.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>226.0</td>\n      <td>548.0</td>\n      <td>130.0</td>\n      <td>58.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>377.0</td>\n      <td>504.0</td>\n      <td>74.0</td>\n      <td>160.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>834.0</td>\n      <td>95.0</td>\n      <td>109.0</td>\n      <td>107.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>26.0</td>\n      <td>144.0</td>\n      <td>124.0</td>\n      <td>117.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "df_idx = df_.set_index(\"image_id\")\n",
    "bbox = df_idx.reset_index(drop=True)\n",
    "bbox.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 56.,  36.],\n       [130.,  58.],\n       [ 74., 160.],\n       ...,\n       [141.,  71.],\n       [184.,  79.],\n       [ 94.,  61.]])"
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "bbox_arr = bbox.to_numpy()\n",
    "w_h = bbox_arr[:,(2,3)]\n",
    "w_h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "tags": [
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend"
    ]
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[array([56., 36.]),\n array([130.,  58.]),\n array([ 74., 160.]),\n array([109., 107.]),\n array([124., 117.])]"
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "anchors = []\n",
    "for b in w_h[:5]:\n",
    "    anchors.append(b)\n",
    "anchors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python36264bityoloconda24058cec4858409782fb7dc648bf693c",
   "display_name": "Python 3.6.2 64-bit ('yolo': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}